import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm'; 
import { User } from './user.entity';
import { CreateUserDto } from './create-user.dto';

@Injectable()
export class UserService {
   constructor(
    @InjectRepository(User)
    private readonly userRepository: Repository<User>,) {
      
   }

   //根据用户ID查找单个用户
   findOne(id: number): Promise<User> {
      return this.userRepository.findOneBy({ id });
   }

   findAll(): Promise<User[]> {
      return this.userRepository.find();
   }

   create(createUserDto: CreateUserDto):Promise<User>{
      const user = new User();
      user.username = createUserDto.username;
      user.password = createUserDto.password;
      user.role = createUserDto.role;
      user.avatar = createUserDto.avatar;
      user.active = 1;
      user.nickname = createUserDto.nickname;

      return this.userRepository.save(user);
   }

   remove(id: number): Promise<void> {
      return this.userRepository.delete(id).then(() => null);
   }
   
}